package cn.portx.view;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf3.dorado.jpa.JpaUtil;
import com.bstek.bdf3.dorado.jpa.policy.SaveContext;
import com.bstek.bdf3.dorado.jpa.policy.impl.SmartSavePolicyAdapter;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.core.Configure;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import cn.portx.entity.Carrier;
import cn.portx.entity.Course;
import cn.portx.entity.Port;
import cn.portx.entity.ShippingCompany;
import cn.portx.entity.Vessel;

@Component
public class ShippingCompanyPR {

	@DataProvider
	@Transactional(readOnly = true)
	public void loadShippingCompanies(Page<ShippingCompany> page, Criteria criteria) {
		JpaUtil.linq(ShippingCompany.class).where(criteria).paging(page);
	}

	@DataProvider
	@Transactional(readOnly = true)
	public void loadVessels(Page<Vessel> page, Criteria criteria, String scId) {
		JpaUtil.linq(Vessel.class).equal("shippingCompanyId", scId).where(criteria).paging(page);
	}

	@DataProvider
	@Transactional(readOnly = true)
	public void loadCourses(Page<Course> page, Criteria criteria, String vesselId) {
		JpaUtil.linq(Course.class).collect(Port.class, "startPortId", "endPortId").equal("vesselId", vesselId).where(criteria).paging(page);
	}

	@DataProvider
	@Transactional(readOnly = true)
	public void loadPorts(Page<Port> page, Map<String, Object> params) {
		if (params == null) {
			params = new HashMap<String, Object>();
		}
		String filterValue = (String) params.get("filterValue");
		JpaUtil.linq(Port.class).addIf(filterValue).or().like("name", filterValue + "%").like("code", filterValue + "%").end().endIf().paging(page);
	}

	@DataResolver
	@Transactional
	public void saveShippingCompanies(List<ShippingCompany> list) {
		JpaUtil.save(list, new SmartSavePolicyAdapter() {
			@Override
			public boolean beforeInsert(SaveContext context) {
				Object parent = context.getParent();
				Object entity = context.getEntity();
				if (parent instanceof ShippingCompany && entity instanceof Vessel) {
					((Vessel) entity).setShippingCompanyId(((ShippingCompany) parent).getId());
				} else if (parent instanceof Vessel && entity instanceof Course) {
					((Course) entity).setVesselId(((Vessel) parent).getId());
				}
				return true;
			}
		});
	}

	@Expose
	@Transactional
	public void deleteShippingCompany(String id) {
		List<String> ids = JpaUtil.linq(Vessel.class, String.class).selectId().equal("shippingCompanyId", id).list();
		if(ids!=null&&ids.size()>0){
			JpaUtil.lind(Course.class).in("vesselId", ids.toArray()).delete();
		}
		JpaUtil.lind(Vessel.class).equal("shippingCompanyId", id).delete();
		JpaUtil.lind(ShippingCompany.class).idEqual(id).delete();
	}

	@Expose
	@Transactional
	public void deleteVessel(String id) {
		JpaUtil.lind(Vessel.class).idEqual(id).delete();
	}

	@Expose
	@Transactional
	public void deleteCourse(String id) {
		JpaUtil.lind(Course.class).idEqual(id).delete();
	}

	@Expose
	@Transactional
	public void initCompany() throws Exception {
		if ("debug".equals(Configure.getString("core.runMode"))) {
			org.springframework.core.io.Resource fileRource = new ClassPathResource("船公司.xls");
			Workbook wb = WorkbookFactory.create(fileRource.getInputStream());
			Sheet sheet = wb.getSheetAt(0);
			for (int m = sheet.getFirstRowNum() + 1; m < sheet.getLastRowNum() + 1; m++) {
				Row row = sheet.getRow(m);
				ShippingCompany sc = new ShippingCompany();
				sc.setName(StringUtils.trim(row.getCell(0).getStringCellValue()));
				sc.setCode(StringUtils.trim(row.getCell(1).getStringCellValue()));
				JpaUtil.persist(sc);
			}

			fileRource = new ClassPathResource("箱属.xls");
			wb = WorkbookFactory.create(fileRource.getInputStream());
			sheet = wb.getSheetAt(0);
			for (int m = sheet.getFirstRowNum() + 1; m < sheet.getLastRowNum() + 1; m++) {
				Row row = sheet.getRow(m);
				Carrier sc = new Carrier();
				sc.setName(StringUtils.trim(row.getCell(0).getStringCellValue()));
				sc.setCode(StringUtils.trim(row.getCell(1).getStringCellValue()));
				JpaUtil.persist(sc);
			}

			fileRource = new ClassPathResource("港口.xls");
			wb = WorkbookFactory.create(fileRource.getInputStream());
			sheet = wb.getSheetAt(0);
			for (int m = sheet.getFirstRowNum() + 1; m < sheet.getLastRowNum() + 1; m++) {
				Row row = sheet.getRow(m);
				Port sc = new Port();
				sc.setName(StringUtils.trim(row.getCell(1).getStringCellValue()));
				sc.setCode(StringUtils.trim(row.getCell(0).getStringCellValue()));
				JpaUtil.persist(sc);
			}
		}
	}
}
